<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/9/21
 * Time: 21:22
 */
namespace app\admin\controller;
use app\models\AllocationCampusModel;
use app\models\AllocationOnlyModel;
use app\models\AuthGroupAccessModel;
use app\models\AuthModel;
use app\models\CampusModel;
use app\models\CompanyModel;
use app\models\ContractModel;
use app\models\StudentModel;
use app\models\UserModel;

/**
 * Class Index
 * @package app\admin\controller
 */
class Index extends Common
{
    /**
     * 首页
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $user = session('user');
        $group_access_model = new AuthGroupAccessModel();
        $menus = $group_access_model->getUserRules($user['id']);
        $groupName = AuthModel::get(get_current_group_id())['title'];
        /**集团*/
        if(get_current_type() == 2){
            $company_name = CompanyModel::get(['id' => $user['company_id']])['company_name'];  //集团名称
        }else{
            $company_name = '';
        }

        /**区域*/
        if(get_current_type() == 3){
            $regional_name = $user['regional_name'];  //区域名称
        }else{
            $regional_name = '';
        }

        /**校区*/
        if(get_current_type() == 4){
			$campus_name = get_current_campus_name();
		}else{
			$campus_name = '';
		}
		$this->assign('campus_name', $campus_name);
        $this->assign('company_name', $company_name);
        $this->assign('regional_name', $regional_name);
        $this->assign('menus', $menus);
        $this->assign('name', $groupName);
        return view();
    }

	//获取当月第一天和最后一天
	function getthemonth($date)
	{
		$firstday = date('Y-m-01', strtotime($date));
		$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
		return array($firstday,$lastday);
	}

    public function welcome()
    {
		$where = [];
		$where_amount_today = [];
		$where_follow = [];
		$where_newly_added = [];
		$where_month_register = [];
		$where_amount_month = [];
		$date = date('Y-m-d');
		$start = $date.' 00:00:00';
		$end = $date.' 23:59:59';

		//今日签单数
		$where['signing_date'] = $date;
		if(get_current_type() == 5){
			$where['campus_id'] = get_current_campus_id();
		}else{
			$where['campus_id'] = ['in',get_current_campus_id()];
		}

		if(get_current_type() == 5){  //员工级别
			$where['contractor'] = get_current_id();
		}

		$today_num = (new ContractModel())->where($where)->count();

		//今日签单金额
		$where_amount_today['signing_date'] = $date;
		if(get_current_type() == 5){
			$where_amount_today['campus_id'] = get_current_campus_id();
		}else{
			$where_amount_today['campus_id'] = ['in',get_current_campus_id()];
		}

		if(get_current_type() == 5){  //员工级别
			$where_amount_today['contractor'] = get_current_id();
		}

		$today_amount_total = ((new ContractModel())->where($where_amount_today)->sum('total_amount') / 100); //总额

		//今日跟进客户
		$where_follow['next_follow_up_record'] = $date;
		if(get_current_type() == 5){
			$where_follow['campus_id'] = get_current_campus_id();
		}else{
			$where_follow['campus_id'] = ['in',get_current_campus_id()];
		}

		if(get_current_type() == 5){  //员工级别
			$where_follow['user_id'] = get_current_id();
		}

		$today_follow = (new StudentModel())->where($where_follow)->count();


		//今日新增用户
		$where_newly_added['create_time'] = array('between', array($start,$end));
		if(get_current_type() == 5){
			$where_newly_added['campus_id'] = get_current_campus_id();
		}else{
			$where_newly_added['campus_id'] = ['in',get_current_campus_id()];
		}

		if(get_current_type() == 5){  //员工级别
			$where_newly_added['user_id'] = get_current_id();
		}

		$today_newly_added = (new StudentModel())->where($where_newly_added)->count();

		$month_day = $this->getthemonth($date);
		$start_month_day = $month_day[0]; //本月第一天
		$end_month_day = $month_day[1];  //本月最后一天

		//本月签单数量
		$where_month_register['signing_date'] = array('between', array($start_month_day,$end_month_day));
		if(get_current_type() == 5){
			$where_month_register['campus_id'] = get_current_campus_id();
		}else{
			$where_month_register['campus_id'] = ['in',get_current_campus_id()];
		}

		if(get_current_type() == 5){  //员工级别
			$where_month_register['contractor'] = get_current_id();
		}

		$month_num = (new ContractModel())->where($where_month_register)->count();

		//本月签单金额
		$where_amount_month['signing_date'] =  array('between', array($start_month_day,$end_month_day));
		if(get_current_type() == 5){
			$where_amount_month['campus_id'] = get_current_campus_id();
		}else{
			$where_amount_month['campus_id'] = ['in',get_current_campus_id()];
		}

		if(get_current_type() == 5){  //员工级别
			$where_amount_month['contractor'] = get_current_id();
		}
		$month_amount_total = ((new ContractModel())->where($where_amount_month)->sum('total_amount') / 100); //总额

		//欠费人数
		$where_student = [];
		if(get_current_type() == 5){
			$where_student['campus_id'] = get_current_campus_id();
		}else{
			$where_student['campus_id'] = ['in',get_current_campus_id()];
		}
		$where_student['arrears_status'] = 0;

		if(get_current_type() == 5){  //员工级别
			$where_student['user_id'] = get_current_id();
		}
		$arrears_num = (new StudentModel())->where($where_student)->count();
		//校区业绩
		$where_campus = [];
		$allocationcampus = [];
		if(get_current_type() == 5){
			$where_campus['id'] = get_current_campus_id();
		}else{
			$where_campus['id'] = ['in',get_current_campus_id()];
		}
		$where_campus['status'] = 1;
		$campus_result = (new CampusModel())->where($where_campus)->select();
		$achievement_campus_amount = 0;
		$achievement_campus_name = '暂无';
		$allocationcampus['create_time'] = array('between', array($start_month_day,$end_month_day));
		foreach ($campus_result as $key => $value){
			$allocationcampus['campus_id_all']  = $value['id'];
			$campus_money = (new AllocationCampusModel())->where($allocationcampus)->sum('rate_all_money'); //校区业绩
			if($campus_money > $achievement_campus_amount){
				$achievement_campus_amount = $campus_money;
				$achievement_campus_name = $value['campus_name'];
			}
		}

		//个人业绩
		$where_only = [];
		$only_name = (new UserModel())->where(['company_id' => get_current_company_id()])->select();
		$achievement_only_amount = 0;
		$achievement_only_name = '暂无';
		$where_only['create_time'] = array('between', array($start_month_day,$end_month_day));
		foreach ($only_name as $key => $value){
			$where_only['payee_only']  = $value['id'];
			if(get_current_type() == 5){
				$where_only['campus_id'] = get_current_campus_id();
			}else{
				$where_only['campus_id'] = ['in',get_current_campus_id()];
			}
			$only_money = (new AllocationOnlyModel())->where($where_only)->sum('rate_only_money'); //个人业绩
			if($only_money > $achievement_only_amount){
				$achievement_only_amount = $only_money;
				$achievement_only_name = $value['name'];
			}
		}
		//先定义一个数组
		$weekarray = ['日', '一', '二', '三', '四', '五', '六'];
		$this->dictionary();
		$this->assign('today_num', $today_num);
		$this->assign('today_follow', $today_follow);
		$this->assign('today_amount_total', $today_amount_total);
		$this->assign('month_amount_total', $month_amount_total);
		$this->assign('today_newly_added', $today_newly_added);
		$this->assign('month_num', $month_num);
		$this->assign('arrears_num', $arrears_num);
		$this->assign('achievement_campus_name', $achievement_campus_name);
		$this->assign('achievement_only_name', $achievement_only_name);
		$this->assign('time', date('Y-m-d'));
		$this->assign('week', $weekarray[date('w')]);
		return $this->fetch();
    }
}
